package edu.must.uh.servlet;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.jasper.tagplugins.jstl.core.If;
import org.apache.log4j.Logger;
import edu.must.uh.impl.AttendancdImpl;
import edu.must.uh.impl.CardNumImpl;
import edu.must.uh.impl.RegCodeImpl;
import edu.must.uh.impl.UserImpl;
import edu.must.uh.util.R;
import edu.must.uh.util.ServletUtils;
import edu.must.uh.util.Tools;
import edu.must.uh.util.ToolsOfDate;
import edu.must.uh.util.ToolsOfString;

@WebServlet("/AttendancdServlet")
public class AttendancdServlet extends BaseServlet {
    private static final long serialVersionUID = 12197442526341123L;
    private static Logger logger = Logger.getLogger(AttendancdServlet.class);  
    
    
    
    public Object getList(HttpServletRequest request, HttpServletResponse response) throws Exception {  
        AttendancdImpl attendancdImpl = new AttendancdImpl();
        Map<String, Object> searchcondition = ServletUtils.getsearchcondition(request);
        List<Map<String,Object>>resList = attendancdImpl.getList(searchcondition, " ORDER BY CREATETIME DESC");
        if(resList !=null && !resList.isEmpty()) {
            Integer indexInteger = resList.size();
            for (Map<String, Object> map : resList) {
                map.put("XUHAO", indexInteger--);                 
                ServletUtils.createTimeHelper(map,"ATTEND_TIME"); 
            }
        }           
        return R.succ(resList);         
    }
    
    public Object getAttendancdDay(HttpServletRequest request, HttpServletResponse response) throws Exception {  
        AttendancdImpl attendancdImpl = new AttendancdImpl();
        UserImpl UserImpl = new UserImpl();
        List<Map<String,Object>>  resList = new ArrayList<Map<String,Object>>();
     
        String _startDate =  ToolsOfDate.getCurrFirstMonthDate();        
      //  String _endDate = ToolsOfDate.getCurrDate();        
        String UserId = ToolsOfString.NulltoEmpty(request.getParameter("UserId"));
        String sqlString = "SELECT UserId, Account,RealName FROM base_user where UserId ='"+UserId+"'";
        Map<String,Object> userMap =  UserImpl.getMap(sqlString);
       // Integer days = ToolsOfDate.getDaysBetweenDatesString(_startDate, _endDate, "yyyy-MM-dd");
        if(Tools.isEmpty(UserId) || userMap == null) {
        	return R.fail("请选择打卡人员");
        }
        for(int i = 0 ; i < 32;i++) {
        	Map<String,Object> Map = new HashMap<String,Object>();
        	Map.putAll(userMap);
        	String _Date=ToolsOfDate.getNowDateAddDays(_startDate,i); 
        	Map.put("AttendancdDate",_Date);
        	sqlString = "SELECT ATTEND_TIME FROM attendance_recd where ATTEND_DATE ='"+_Date+"'  and UserId ='"+UserId+"' and WORK_TYPE = 1 and DELETEMARK = 0 ORDER BY ATTEND_TIME,CREATETIME DESC";
        	Map<String,Object> OnTimeMap =  attendancdImpl.getMap(sqlString);
        	if(OnTimeMap != null) {
        		String OnDate = ToolsOfString.NulltoEmpty(OnTimeMap.get("ATTEND_TIME"));
        		String OnTime = ToolsOfDate.toHHMI(ToolsOfDate.StringToTimestamp(OnDate,"YYYY-MM-DD HH:MI:SS.FF"));
        		Map.put("OnTime",OnTime);
        	}
        	
        	sqlString = "SELECT ATTEND_TIME FROM attendance_recd where ATTEND_DATE ='"+_Date+"' and  UserId ='"+UserId+"'and  WORK_TYPE = 2 and DELETEMARK = 0 ORDER BY ATTEND_TIME DESC,CREATETIME DESC";
        	Map<String,Object> OffTimeMap =  attendancdImpl.getMap(sqlString);
        	if(OffTimeMap != null) {
        		String OffDate = ToolsOfString.NulltoEmpty(OffTimeMap.get("ATTEND_TIME"));
        		String OffTime = ToolsOfDate.toHHMI(ToolsOfDate.StringToTimestamp(OffDate,"YYYY-MM-DD HH:MI:SS.FF"));
        		Map.put("OffTime",OffTime);
        	}
        	
        	resList.add(Map);
        }
                 
        return R.succ(resList);         
    }
    
    public Object getListByCardNum(HttpServletRequest request, HttpServletResponse response) throws Exception {      
        
        AttendancdImpl attendancdImpl = new AttendancdImpl();
        CardNumImpl cardNumImpl = new CardNumImpl();
        String CARDNUM =ToolsOfString.NulltoEmpty(request.getParameter("CARDNUM"));
        String UserId =ToolsOfString.NulltoEmpty(request.getParameter("UserId"));
       
        if(Tools.notEmpty(CARDNUM)) {
        	 String sqlString = "SELECT UserId  FROM cardnum Where CARDNUM = '"+CARDNUM+"'";
        	   Map<String, Object> cardNumMap = cardNumImpl.getMap(sqlString);
        	   if(cardNumMap !=null) {
        		   UserId = ToolsOfString.NulltoEmpty(cardNumMap.get("UserId"));
               }
        }
        if(Tools.notEmpty(UserId)) {
            Map<String, Object> searchcondition = new HashMap<String, Object>();
            searchcondition.put("UserId", UserId);            
            List<Map<String,Object>>resList = attendancdImpl.getList(searchcondition, " ORDER BY ATTEND_TIME DESC");
            if(resList !=null && !resList.isEmpty()) {              
                for (Map<String, Object> map : resList) {                              
                    ServletUtils.createTimeHelper(map,"ATTEND_TIME");             
                    
                }
            }           
            return R.succ(resList);
                    
        }       
        return R.fail("无效卡");
               
    }
    
    
    
    
    public Object delete(HttpServletRequest request, HttpServletResponse response) throws Exception {      
        
        RegCodeImpl regCodeImpl = new RegCodeImpl();
        String keyValue =ToolsOfString.NulltoEmpty(request.getParameter("ID")); 
        Integer resInteger = regCodeImpl.delete(keyValue);
        return R.succ(resInteger);         
    }
    
  public Object save(HttpServletRequest request, HttpServletResponse response) throws Exception {  
      
        AttendancdImpl attendancdImpl = new AttendancdImpl();
        CardNumImpl cardNumImpl = new CardNumImpl();
        String CARDNUM =ToolsOfString.NulltoEmpty(request.getParameter("CARDNUM"));
        String UserId =ToolsOfString.NulltoEmpty(request.getParameter("UserId"));  
        String WORK_TYPE =ToolsOfString.NulltoEmpty(request.getParameter("WORK_TYPE"));
        if(Tools.notEmpty(CARDNUM)) {
        	  String sqlString = "SELECT UserId  FROM cardnum Where CARDNUM = '"+CARDNUM+"'";
              Map<String, Object> cardNumMap = cardNumImpl.getMap(sqlString);
              if(cardNumMap !=null) {
            	  UserId = ToolsOfString.NulltoEmpty(cardNumMap.get("UserId"));  
              }
        }
        if(Tools.notEmpty(UserId)) {
	        //CREATETYPE : 1 是登陆 2 是排卡
	        //WORK_TYPE : 1 是上班 2 是下班
	        String sql = "INSERT INTO attendance_recd(UserId,CARDNUM,WORK_TYPE,CREATETIME,CREATETYPE,ATTEND_DATE,ATTEND_TIME)VALUES('"+UserId+"','"+CARDNUM+"','"+WORK_TYPE+"',now(),2,now(),now())";
	        Integer resInteger = attendancdImpl.save(sql);
	        return R.succ(resInteger);
         }
        return R.fail( "卡" + CARDNUM+ "無效");
       
    }
    
    
    
   
}
